package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.CreateStudentDTO;
import com.sky.dto.UpdateStudentDTO;
import com.sky.dto.UpdateStudentPasswordDTO;
import com.sky.entity.Order;
import com.sky.entity.Student;
import com.sky.service.OrderService;
import com.sky.service.StudentService;
import com.sky.vo.OrderVO;
import com.sky.vo.StudentVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/students")
public class StudentController {
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;
    
    @Autowired
    private OrderService orderService;

    @GetMapping("/page")
    public ResponseEntity<IPage<StudentVO>> getStudentList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String studentId,
            @RequestParam(required = false) String status) {
        logger.info("获取学生列表 - 页码: {}, 每页大小: {}, 用户名: {}, 学号: {}, 状态: {}",
                page, size, username, studentId, status);
        IPage<Student> studentPage = new Page<>(page, size);
        IPage<StudentVO> result = studentService.getStudentList(studentPage, username, studentId, status);
        logger.info("获取学生列表成功 - 总数: {}", result.getTotal());
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{id}")
    public ResponseEntity<StudentVO> getStudentById(@PathVariable Long id) {
        logger.info("获取学生信息 - ID: {}", id);
        StudentVO studentVO = studentService.getStudentById(id);
        if (studentVO == null) {
            logger.warn("学生不存在 - ID: {}", id);
            return ResponseEntity.notFound().build();
        }
        logger.info("获取学生信息成功 - ID: {}, 姓名: {}", id, studentVO.getUsername());
        return ResponseEntity.ok(studentVO);
    }

    @PostMapping
    public ResponseEntity<Long> createStudent(@RequestBody CreateStudentDTO createStudentDTO) {
        logger.info("创建学生 - 用户名: {}, 学号: {}",
                createStudentDTO.getUsername(), createStudentDTO.getStudentId());
        Long id = studentService.createStudent(createStudentDTO);
        logger.info("创建学生成功 - ID: {}, 用户名: {}", id, createStudentDTO.getUsername());
        return ResponseEntity.status(HttpStatus.CREATED).body(id);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateStudent(@PathVariable Long id, @RequestBody UpdateStudentDTO updateStudentDTO) {
        logger.info("更新学生信息 - ID: {}", id);
        updateStudentDTO.setId(id);
        studentService.updateStudent(updateStudentDTO);
        logger.info("更新学生信息成功 - ID: {}", id);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable Long id) {
        logger.info("删除学生 - ID: {}", id);
        studentService.deleteStudent(id);
        logger.info("删除学生成功 - ID: {}", id);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<Void> toggleStudentStatus(@PathVariable Long id) {
        logger.info("切换学生状态 - ID: {}", id);
        studentService.toggleStudentStatus(id);
        logger.info("切换学生状态成功 - ID: {}", id);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/password")
    public ResponseEntity<Void> updateStudentPassword(@RequestBody UpdateStudentPasswordDTO updateStudentPasswordDTO) {
        logger.info("修改学生密码 - ID: {}", updateStudentPasswordDTO.getId());
        studentService.updateStudentPassword(updateStudentPasswordDTO);
        logger.info("修改学生密码成功 - ID: {}", updateStudentPasswordDTO.getId());
        return ResponseEntity.noContent().build();
    }
    
    // 获取学生订单列表接口
    @GetMapping("/{studentId}/orders")
    public ResponseEntity<IPage<OrderVO>> getStudentOrders(
            @PathVariable Long studentId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String status) {
        logger.info("获取学生订单列表 - 学生ID: {}, 页码: {}, 每页大小: {}, 状态: {}",
                studentId, page, size, status);
        IPage<Order> orderPage = new Page<>(page, size);
        IPage<OrderVO> result = orderService.getOrderList(orderPage, studentId, status);
        logger.info("获取学生订单列表成功 - 学生ID: {}, 总数: {}", studentId, result.getTotal());
        return ResponseEntity.ok(result);
    }
    
    // 查看订单详情接口
    @GetMapping("/orders/{orderId}")
    public ResponseEntity<OrderVO> getOrderDetail(@PathVariable String orderId) {
        logger.info("查看订单详情 - 订单编号: {}", orderId);
        OrderVO orderVO = orderService.getOrderDetail(orderId);
        if (orderVO == null) {
            logger.warn("订单不存在 - 订单编号: {}", orderId);
            return ResponseEntity.notFound().build();
        }
        logger.info("查看订单详情成功 - 订单编号: {}, 状态: {}", orderId, orderVO.getStatus());
        return ResponseEntity.ok(orderVO);
    }
}
